package xzy.Controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xzy.dao.TeacherMapper;
import xzy.pojo.Student;
import xzy.pojo.Teacher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "teacherResgist_Servlet")
public class teacherResgist_Servlet extends HttpServlet {
    ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    TeacherMapper teacherMapper= (TeacherMapper) context.getBean("teacherMapper");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入注册教师账号功能");
        request.setCharacterEncoding("utf-8");
        String teacherName=request.getParameter("userName");
        String teacherPwd=request.getParameter("password");
        try{
            if(teacherName.length()>0&&teacherPwd.length()>0){
                Teacher teacher=new Teacher(teacherName,teacherPwd);
                int res=teacherMapper.addTeacher(teacher);
                if(res>0){
                    request.setAttribute("Resgist_error","注册成功");
                }else {
                    request.setAttribute("Resgist_error","注册失败");
                }
            }else {
                request.setAttribute("Resgist_error","注册用户名，密码不可以为空");
            }
        }catch (Exception e){
            request.setAttribute("Resgist_error","教师已录入");
        }finally {
            request.getRequestDispatcher("/Regist.jsp").
                    forward(request,response);
        }

    }
}
